// JavaScript Document

function addEvent(sEvent){
	var aEvent = eval (sEvent);
	var aEvents = new Array();
	aEvents.push(aEvent); 
	$('#calendar').fullCalendar('addEventSource',aEvents);
}

function addEvents(sEvent){
	$('#calendar').fullCalendar('removeEvents');
	var aEvents = eval (sEvent); 
	$('#calendar').fullCalendar('addEventSource',aEvents)
}

$(document).ready(function() {
	var oToday = new Date();
	var sMonth = oToday.getMonth()+1;
	
	$('#calendar').fullCalendar({
		theme: true,
		prev: function(event){
			alert("good..");
		}
		,
		eventClick: function(calEvent, jsEvent, view){
			
			window.open('adminEvent.php?evento='+calEvent.id,'','location={no | 0}, titlebar={ no | 0 },status={no | 0},menubar = { no | 0 },toolbar = { no | 0 },left='+((window.innerWidth/2)-200)+',top=100,resizable={ si | 1 },width=500,height=600');
			return false;	
		},
		header: {
			left: 'month',
			center: 'title',
			right: 'prev,next today'
		},
		editable: false,
		events:{ url: 'getEventosByMes.php',type: 'POST',data: {mes: sMonth,  sala: iSala}},//error: function (){alert('Error al cargar los eventos')}},
		dayClick: function(date, allDay, jsEvent, view) {
			if(  date.getFullYear() >= oToday.getFullYear() &&   date.getMonth()+1 >= sMonth && date.getDate()>=oToday.getDate()){
			var dateString = date.getFullYear()+"-"+(parseInt(date.getMonth())+1)+"-"+date.getDate();
			
			window.open('addNewEvent.php?fecha='+dateString+'&sala='+iSala,'','location={no | 0}, titlebar={ no | 0 },status={no | 0},menubar = { no | 0 },toolbar = { no | 0 },left='+((window.innerWidth/2)-200)+',top=100,resizable={ si | 1 },width=500,height=600');
			return false;
			}
    	}	
	});
	
	}
);

function cargarEventosByMes(sMes){
		oXHR = zXmlHttp.createRequest();
		oXHR.open("post", "getEventosByMes.php", true);
		oXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oXHR.onreadystatechange = function () {               
			if (oXHR.readyState == 4) {
				if (oXHR.status == 200) {
					 addEvents(oXHR.responseText);                                
				}
			}
		};   
		oXHR.send("mes="+sMes+"&sala="+iSala);
}

function cargarEventosByMeses(sMes){
		
		oXHR = zXmlHttp.createRequest();
		oXHR.open("post", "getEventosByMeses.php", true);
		oXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		oXHR.onreadystatechange = function () {               
			if (oXHR.readyState == 4) {
				if (oXHR.status == 200) {
					 addEvents(oXHR.responseText);                                
				}
			}
		};   
		oXHR.send("mes="+sMes+"&sala="+iSala);
}

function updateEvent(sEvent){
	var aEvent = eval (sEvent);
	$('#calendar').fullCalendar('removeEvents',aEvent.id);
	$('#calendar').fullCalendar('renderEvent', aEvent, true); 
}

function deleteEvent(sEvent){
	var aEvent = eval (sEvent);
	$('#calendar').fullCalendar('removeEvents',aEvent.id);
}

function goToDate(sFecha){
	var aPartesFecha = sFecha.split('-');
	var iNumPartes = aPartesFecha.length;
	var bAllCorrect = true;
	var sAnio = "",sMes = "",sDia="";
	
	if( iNumPartes > 1 && iNumPartes < 4 ){
		
		if(esEntero(aPartesFecha[0]) && esEntero(aPartesFecha[1])){
			sAnio = aPartesFecha[0]; 
			if(aPartesFecha[1][0] == '0')
				sMes = parseInt(aPartesFecha[1][1]);
			else
				sMes = parseInt(aPartesFecha[1]);
		}else
			bAllCorrect = false;
				
		if(iNumPartes == 3){
			if(esEntero(aPartesFecha[2]))
				sDia=aPartesFecha[2];
			else
				bAllCorrect = false;
		}		
						
		if(bAllCorrect){
			$('#calendar').fullCalendar( 'gotoDate', sAnio, sMes, sDia);
			cargarEventosByMes(sMes);
		}
		else
			alert('Fecha no Valida');
						
	}else
		alert('Fecha no Valida');
}

function esEntero(sNumero){
	var bEsEntero = true;
	if(sNumero != ''){
		if(!(/^([0-9])*$/).test(sNumero))
			bEsEntero = false;
	}else
		bEsEntero = false;
		
	return bEsEntero;
}

$(function loadMeses() {
	$('.fc-button-prev, .fc-button-next').bind('click',function(){
		var d = $('#calendar').fullCalendar('getDate');
		var view = $('#calendar').fullCalendar('getView');
		
		if(view.name == 'month' || view.name == 'agendaDay'){
			cargarEventosByMes(d.getMonth()+1);
		}else if(view.name == 'agendaWeek'){
			cargarEventosByMeses(d.getMonth()+1);
		}
	});
});